#!/bin/sh

LOGFILE=/var/log/petitboot/pb-discover.log
PIDFILE=/var/run/petitboot.pid

PATH=/usr/bin:/usr/sbin:/bin:/sbin
export PATH

verbose=
if pb-config debug | grep -q enabled
then
	verbose=-v
fi

case "$1" in
    start)
        ulimit -c unlimited
        mkdir -p -m 0775 $(dirname $LOGFILE)
        mkdir -p -m 0775 /var/petitboot
        # Set permissions for normal users
        chown -R root:petitgroup $(dirname $LOGFILE)
        chown -R root:petitgroup /var/petitboot

        export $(cat /etc/locale)
        pb-discover -l $LOGFILE $verbose &
        echo $! > $PIDFILE
        ;;
    stop)
        pid=$(cat $PIDFILE)
        [ -n "$pid" ] && kill -TERM $pid
        ;;
    *)
        echo "Usage: $0 {start|stop}"
        exit 1
        ;;
esac

exit 0
